import java.util.Stack;

public class Solution {

    public static boolean isUgly(int n) {
        if(n<=0){
            return false;
        }
        int [] factors={2,3,5};
        for(int factor: factors){
            while(n%factor==0){
                n/=factor;
            }
        }
        return n==1;
    }
    public static boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> stack=new Stack<>();
        int j=0;
        for (int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while(!stack.empty()&&j< popV.length&&stack.peek()==popV[j]){
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }
    public static  int search(int key,int []arr){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]==key){
                return i;
            }
        }
        return -1;

    }

    public  static int[] nextGreaterElement(int[] nums1, int[] nums2) {

        int j = 0;
        for (int i = 0; i < nums1.length; i++) {
            int index = search(nums1[i], nums2);
            for (j = index + 1; j < nums2.length; j++) {
                if (nums2[index] < nums2[j]) {
                    nums1[i] = nums2[j];
                    break;
                }
            }
            if (j == nums2.length) {
                nums1[i] = -1;
            }
        }
        return nums1;

    }
    public  static boolean IsPopOrder1 (int[] pushV, int[] popV){
        Stack <Integer>stack=new Stack<>();
        int j=0;
        for (int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while (!stack.empty()&&j<popV.length&&popV[j] == stack.peek()) {
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }
}
